﻿using System;
using System.Collections.Generic;
using IM.GUI.ContactList;
using System.Windows.Forms;
using IM.Interfaces.GUI;
using IM.GUI.Conversation;

namespace IM.GUI.Widgets
{
	/// <summary>
	/// Provider for controls in Widgets library.  Can be user to access widget instances.
	/// Needs to be activated before using widgets.
	/// </summary>
	/// <remarks>
	/// this class is singleton
	/// </remarks>
	class WidgetsProvider
	{
		private IContactList contactList;
		private IConversation conversation;

		public IConversation Conversation
		{
			get { return conversation; }
		}

		public IContactList ContactList
		{
			get { return contactList; }
		}

		static WidgetsProvider instance;

		/// <summary>
		/// if not activated, activate WidgetsProvider 
		/// </summary>
		/// <returns>
		/// A <see cref="WidgetsProvider"/>
		/// </returns>
		public static WidgetsProvider Activate()
		{
			if (instance == null)
				instance = new WidgetsProvider();

			return instance;
		}

		private WidgetsProvider()
		{
			// register for creation events
			ContactListWindow.OnControlCreated += new ControlEventHandler(OnContactListWindowCreated);
			ConversationWindow.OnControlCreated += new ControlEventHandler(ConversationWindow_OnControlCreated);
		}

		void ConversationWindow_OnControlCreated(object sender, ControlEventArgs e)
		{
			this.conversation = e.Control as IConversation;
		}

		private void OnContactListWindowCreated(object sender, ControlEventArgs e)
		{
			this.contactList = e.Control as IContactList;
		}
	}
}
